<?php

namespace app\controller;

use app\BaseController;

class Post extends BaseController
{
    //拼接参数
    public static function params($url, $data)
    {
        $openUrl = env('main.main_openUrl', 'https://main.miniappss.com/');
        $APIID = env('main.main_apiid', '93VGNQRG5MYBU0');
        $APIKEY = env('main.main_apikey', 'l07g8r45m89xkak0');
        $APIIV = env('main.main_apiiv', 'qb82jd75anjxdxka');
        $sign = Sign::Sign($data, $APIKEY);
        $params = [
            "api_id" => $APIID,
            "sign" => $sign,
            "data" => $data
        ];
        $params = json_encode($params);
        [$bool, $data] = self::Post([
            'url' => $openUrl . $url,
            'method' => 'POST',
            'header' => ['Content-Type: application/json'],
            'data' => $params
        ]);
        return $bool ? [true, $data] : [false, $data];
    }
    //发送请求
    public static function post($rq)
    {
        $curlHandle = curl_init();
        curl_setopt($curlHandle, CURLOPT_URL, $rq['url']);
        switch (true) {
            case isset($rq['method']) && in_array(strtolower($rq['method']), array('get', 'post', 'put', 'delete', 'head')):
                $method = strtoupper($rq['method']);
                break;
            case isset($rq['data']):
                $method = 'POST';
                break;
            default:
                $method = 'GET';
        }
        $header = isset($rq['header']) ? $rq['header'] : array();
        $header[] = 'Method:' . $method;
        $header[] = "User-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)";
        isset($rq['host']) && $header[] = 'Host:' . $rq['host'];
        curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, $method);
        isset($rq['timeout']) && curl_setopt($curlHandle, CURLOPT_TIMEOUT, $rq['timeout']);
        isset($rq['data']) && in_array($method, array('POST', 'PUT')) && curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $rq['data']);
        $ssl = substr($rq['url'], 0, 8) == "https://" ? true : false;
        if (isset($rq['cert'])) {
            curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, true);
            curl_setopt($curlHandle, CURLOPT_CAINFO, $rq['cert']);
            curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, 2);
            if (isset($rq['ssl_version'])) {
                curl_setopt($curlHandle, CURLOPT_SSLVERSION, $rq['ssl_version']);
            } else {
                curl_setopt($curlHandle, CURLOPT_SSLVERSION, 4);
            }
        } else if ($ssl) {
            curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);   //true any ca
            curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, 2);       //check only host
            if (isset($rq['ssl_version'])) {
                curl_setopt($curlHandle, CURLOPT_SSLVERSION, $rq['ssl_version']);
            } else {
                curl_setopt($curlHandle, CURLOPT_SSLVERSION, 4);
            }
        }
        $return['content'] = curl_exec($curlHandle);
        $return['result'] = curl_getinfo($curlHandle);
        curl_close($curlHandle);
        $response = json_decode($return['content'], true);
        if ($response) {
            if ($response['code'] == 200) {
                return [true, $response['data']];
            } else {
                return [false, $response];
            }
        }
        return [false, '查询失败'];
    }
}
